package com.bcu.team.comment.utils;

import cn.hutool.crypto.asymmetric.RSA;
import com.bcu.team.comment.entity.TokenInfo;
import com.bcu.team.comment.entity.User;
import com.bcu.team.comment.exception.InvalidTokenException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    private Logger logger= LoggerFactory.getLogger(getClass());
    private static RSA rsa=new RSA(RSAUtils.getKeyFromFile("key.pri"), RSAUtils.getKeyFromFile("key.pub"));

   public static String createJwtToken(User u)
    {

        Map claims= new HashMap<>();
//        claims.put("userId",u.getUserId());
//        claims.put("userStatus",u.getUserStatus());
//        //claims.put("userOpenId",u);
//        claims.put("userType",u.getUserType());
//        claims.put("userOldStatus",u.getUserOldStatus());
//        claims.put("userName",u.getUserInfo().getUserName());
//        claims.put("userSex",u.getUserInfo().getUserSex());
//        claims.put("userOrigin",u.getUserInfo().getUserOrigin());
//        claims.put("userClassName",u.getUserInfo().getUserClassName());
//        claims.put("userPartName",u.getUserInfo().getUserPartName());
//        claims.put("userLevel",u.getUserInfo().getUserLevel());
//        claims.put("userEmail",u.getUserInfo().getUserEmail());
//        claims.put("userPhone",u.getUserInfo().getUserPhone());
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(new Date(new Date().getTime()+2*60*60*1000))
                .setSubject(u.getUserId().toString())
                .signWith(rsa.getPrivateKey())
                .compact();
    }

    public static Claims resolveToken(String token)throws Exception {
        Jws<Claims> jws;
            jws = Jwts.parserBuilder()  // (1)
                    .setSigningKey(rsa.getPublicKey())         // (2)
                    .build()                    // (3)
                    .parseClaimsJws(token); // (4)
                    return jws.getBody();
            // we can safely trust the JWT
    }

    public static TokenInfo getTokenInfo(String token){
        try {
            Claims claims = resolveToken(token);
            TokenInfo tokenInfo=new TokenInfo();
//                tokenInfo.setUserId((String)claims.get("userId"));
//                //tokenInfo.setUserOpenId((String)claims.get("userOpenId"));
//                tokenInfo.setUserStatus((String)claims.get("userStatus"));
//                tokenInfo.setUserType((String)claims.get("userType"));
//                tokenInfo.setUserOldStatus((String)claims.get("userOldStatus"));
//                tokenInfo.setUserName((String)claims.get("userName"));
//                tokenInfo.setUserSex((String)claims.get("userSex"));
//                tokenInfo.setUserOrigin((String)claims.get("userOrigin"));
//                tokenInfo.setUserClassName((String)claims.get("userClassName"));
//                tokenInfo.setUserPartName((String)claims.get("userPartName"));
//                tokenInfo.setUserLevel((String)claims.get("userLevel"));
//                tokenInfo.setUserEmail((String)claims.get("userEmail"));
//                tokenInfo.setUserPhone((String)claims.get("userPhone"));
            return tokenInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new InvalidTokenException();
        }

    }
}

